import {navigate} from '../../utils/util.js'
import { buy, application } from '../../api/index.js'

// pages/buy/buy.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    applyParams: {
      name: '',
      card_number: '',
      amount: 0,
      region: ''
    },
    range: [],
    active: false,
    identity: '',
    limit: 0,
    regionName: '',
    application: '' //申请须知
  },

  onReady: function() {
  },

  onShow: function() {
    this.initLimit()
    this.getRegionStatus()
    this.fetchApplication()
  },

  fetchApplication: function() {
    application().then(res => {
      this.setData({
        application: res.data.application
      })
    }).catch(e => {
      console.log(e, '申请须知错误')
    })
  },

  navigaTo: function(e) {
    navigate(e)
  },

  buy: function() {
    const flag = this.vaild()
    const { applyParams} = this.data

    if(flag) {
      buy(applyParams)
      .then(res => {
        wx.showToast({
          title: '购买成功'
        })
      })
      .catch(e => {
        wx.showToast({
          title: '购买失败',
          icon: 'none'
        })
        console.log(e, '购买机器')
      })

    }

  },

  changeNameWidthId: function(e) {
    let {applyParams} = this.data
    const {detail: {value}, currentTarget:{dataset:{name}}} = e
    console.log(e)
    if(name === 'name') {
      applyParams = Object.assign({}, applyParams, { name: value })

      this.setData({
        applyParams
      }, () => {
        console.log(this.data.applyParams, 999)
      })
    }

    if(name === 'idCard') {
      applyParams = Object.assign({}, applyParams, { card_number: value })

      this.setData({
        applyParams
      }, () => {
      })
    }
    
  },

  vaild: function() {
    const identity = wx.getStorageSync('identity')
    const {applyParams:{name, card_number, amount, region}} = this.data

    if(!name) {
      wx.showToast({
        title: '请输入姓名',
        icon: 'none'
      })
      return
    }

    if(!card_number) {
      wx.showToast({
        title: '请输入身份证号',
        icon: 'none'
      })
      return
    }

    if (identity === 'REGION') {
      if(!region) {
        wx.showToast({
          title: '请选择区域服务商',
          icon: 'none'
        })
        return
      }
    }

    if (!amount) {
      wx.showToast({
        title: '至少购买1个',
        icon: 'none'
      })
      return
    }

    return true
  },

  change: function(e) {
    let { applyParams} = this.data
    const {detail:{count}} = e
    applyParams = Object.assign({}, applyParams, { amount: count})
    this.setData({
      applyParams
    },() => {
      console.log(this.data.applyParams)
    })
  },

  initLimit: function() {
    let { applyParams } = this.data
    const {limit} = this.data
    const cacheLimit = wx.getStorageSync('limit')
    const regionName = wx.getStorageSync('regionName')

    applyParams = Object.assign({}, applyParams, { amount: cacheLimit || 0})

    this.setData({
      range: [cacheLimit || 0, 999],
      applyParams,
      limit: cacheLimit,
      regionName
    },() => {
      console.log(this.data.range, 'buy range')
    })
  },

  getRegionStatus: function() {
    const active = wx.getStorageSync('active')
    const identity = wx.getStorageSync('identity')

    this.setData({
      active,
      identity
    })
  },

  onUnload: function() {
    wx.setStorageSync('regionName', '')
    wx.setStorageSync('limit', 0)
  }
})